load("@rules_pkg//:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
load("@rules_pkg//:install.bzl", "pkg_install")
load("//:defs.bzl", "codeql_platform")
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
load("//misc/bazel/cmake:cmake.bzl", "generate_cmake")

filegroup(
    name = "schema",
    srcs = ["schema.py"],
    visibility = ["//swift:__subpackages__"],
)

filegroup(
    name = "schema_includes",
    srcs = glob(["*.dbscheme"]),
    visibility = ["//swift:__subpackages__"],
)

filegroup(
    name = "codegen_conf",
    srcs = ["codegen.conf"],
    visibility = ["//swift:__subpackages__"],
)

pkg_files(
    name = "dbscheme_files",
    srcs = [
        "ql/lib/swift.dbscheme.stats",
        "//swift/extractor/trap:generated_dbscheme",
    ],
)

pkg_files(
    name = "manifest",
    srcs = ["codeql-extractor.yml"],
)

pkg_filegroup(
    name = "extractor-pack-generic",
    srcs = [
        ":manifest",
        "//swift/tools",
    ] + select({
        "@platforms//os:windows": [],
        "//conditions:default": [
            ":dbscheme_files",
            "//swift/downgrades",
        ],
    }),
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "extractor",
    srcs = ["//swift/extractor:pkg"],
    prefix = "tools/" + codeql_platform,
)

pkg_runfiles(
    name = "swift-autobuilder",
    srcs = ["//swift/swift-autobuilder"],
    prefix = "tools/" + codeql_platform,
)

pkg_runfiles(
    name = "diagnostics",
    srcs = ["//swift/tools/diagnostics:autobuilder-incompatible-os"],
    prefix = "tools/" + codeql_platform,
)

pkg_filegroup(
    name = "resource-dir-arch",
    srcs = ["//swift/third_party/swift-llvm-support:swift-resource-dir"],
    prefix = "resource-dir/" + codeql_platform,
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "extractor-pack-arch",
    srcs = select({
        "@platforms//os:windows": [],
        "//conditions:default": [
            ":extractor",
            ":resource-dir-arch",
        ],
    }) + select({
        "@platforms//os:macos": [
            ":swift-autobuilder",
        ],
        "//conditions:default": [
            ":diagnostics",
        ],
    }),
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "extractor-pack",
    srcs = [
        ":extractor-pack-arch",
        ":extractor-pack-generic",
    ],
    visibility = ["//visibility:public"],
)

pkg_install(
    name = "_create_extractor_pack",
    srcs = ["//swift:extractor-pack"],
)

py_binary(
    name = "create-extractor-pack",
    srcs = ["create_extractor_pack.py"],
    main = "create_extractor_pack.py",
    deps = [":_create_extractor_pack"],
)

# TODO this is unneeded here but still used in the internal repo. Remove once it's not
generate_cmake(
    name = "cmake",
    targets = [
        "//swift/extractor:extractor.real",
        "//swift/logging/tests/assertion-diagnostics:assert-false",
    ] + select({
        "@platforms//os:linux": ["//swift/tools/diagnostics:autobuilder-incompatible-os"],
        "@platforms//os:macos": ["//swift/swift-autobuilder"],
    }),
    visibility = ["//visibility:public"],
)
